//
//  LatestJokeViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/16.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "MyJokeViewController.h"

@interface MyJokeViewController ()
@property (nonatomic, strong) NSMutableArray *JokesArray;

@property (nonatomic, strong) NSMutableDictionary *deleteDic;

//yep8yrgtu92utg9put5opguirugirgurieyuitgju
@end

@implementation MyJokeViewController

//夫妇可共同反馈和关键看老公回家看看了家里可好看了

- (void)viewDidLoad {
    [super viewDidLoad];
    self.deleteDic = [NSMutableDictionary dictionary];
    BlurImageView *blurimageView = [[BlurImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.tableView.backgroundView = blurimageView;
    UIImage * image = [[UIImage imageNamed:@"delete"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * edit = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(clickRight:)];
    self.navigationItem.rightBarButtonItem = edit;
     self.tableView.separatorStyle =  0;
    [self.tableView registerClass:[JokeTableCell class] forCellReuseIdentifier:@"jokeCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickRight:(UIBarButtonItem *)button {

    [self.tableView setEditing:!self.tableView.editing animated:YES];

    if (self.tableView.editing == YES) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"rubbish"]imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(delete:)];
        
        
    }
    else{self.navigationItem.leftBarButtonItem = nil;
    
    }
    
    
    [self.tableView reloadData];
    
    
}

- (void)delete:(UIBarButtonItem *)button {

    for (id obj in self.deleteDic) {
        
    LatestJokeModel *model = self.deleteDic[obj];
    [[PlayerDataBase shareDataBase] deleteJokesWithJokeName:  model.content];
        
    }
    [self.deleteDic removeAllObjects];
    [self.tableView reloadData];

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return  [[PlayerDataBase shareDataBase] selegetAllFromJokeTable].count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  JokeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jokeCell" forIndexPath:indexPath];
    
    
    
    
    self.JokesArray = [[PlayerDataBase shareDataBase] selegetAllFromJokeTable];
   
    NSArray * arr = @[@"开心一刻送给珍贵的你࿐",@"笑死你不偿命࿐",@"你有这些智慧么࿐",@"放松一些,体会精彩人生࿐",@"更多精彩尽在笑笑天气࿐"];
    int a = arc4random()%5;
    cell.showLabelOne.title.text = arr[a];
    LatestJokeModel  *JokesModel = self.JokesArray[indexPath.row];
    //  cell.showLabelOne.title.text = arr[indexPath.row];
    
    if (JokesModel.content.length == 0) {
          cell.JokesIntroduce.text = @"该条数据消失了...肿么办";
//        cell.JokesIntroduce.font = [UIFont boldSystemFontOfSize:20];
//        cell.JokesIntroduce.textAlignment = 1;
      
    }
    else {
        cell.JokesIntroduce.text = JokesModel.content;
        cell.showLabelTwo.selected = YES;
}
    
//    [cell.showLabelTwo addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)];
   // cell.showLabelTwo.selected = NO;
   
    // Configure the cell..
       // Configure the cell...
 //   cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
    
    if (self.tableView.editing == YES) {
        cell.selectionStyle = 3;
    }
    else {cell.selectionStyle = 0;}
    cell.shareButton.hidden = YES;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kWidth/2;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    // [self.tableView setEditing:YES animated:YES];
    return YES;
    
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSMutableArray *modelArray = [[PlayerDataBase shareDataBase] selegetAllFromJokeTable];
    
    LatestJokeModel *model =  modelArray[indexPath.row];
    
    
    [[PlayerDataBase shareDataBase] deleteJokesWithJokeName:model.content];
    
    
    [tableView reloadData];
    
}

//- (NSMutableDictionary *)deleteDic {
//    if (!_deleteDic) {
//        _deleteDic = [[NSMutableDictionary alloc] init];
//    }
//
//    return _deleteDic;
//
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // NSMutableArray *modelArray = [[PlayerDataBase shareDataBase] selegetAllFromJokeTable];
    if (self.tableView.editing == YES) {
        
        [self.deleteDic setObject:self.JokesArray[indexPath.row] forKey:indexPath];

    }
    else {
        
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    // NSMutableArray *modelArray = [[PlayerDataBase shareDataBase] selegetAllFromJokeTable];
    if (self.tableView.editing == YES) {
        [self.deleteDic removeObjectForKey:indexPath];
    }
    
}

//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"事实上";}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
